/**
 * Excel工具类
 */
        
package system.imp;

import java.util.*;

import framework.core.*;
import java.io.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.*;

public class ExcelData 
{
    private Vector mColumns = new Vector();
    
    private Vector mRows = new Vector() ;

    private boolean isMustColumn(int idx)
    {
        ExcelColumn column = (ExcelColumn) mColumns.get(idx);
        return column.isMust();
    }
    private int getColumnIndex(String name)
    {
        for(int i=0;i<mColumns.size();i++)
        {
            ExcelColumn column = (ExcelColumn) mColumns.get(i);
            if(column.getName().equalsIgnoreCase(name)) return i ;
        }
        return -1;
    }
    private String getColumnName(int idx)
    {
        return ((ExcelColumn)mColumns.get(idx)).getName();
    }
    
    public ExcelData(FileHolder file) throws Exception
    {
        InputStream stream = new java.io.ByteArrayInputStream(file.getBytes());
        POIFSFileSystem f = new POIFSFileSystem(stream);
        HSSFWorkbook wb = new HSSFWorkbook(f);
        HSSFSheet sheet = wb.getSheetAt(0);     
        HSSFRow row = null;
        HSSFCell cell = null ;
        Iterator ir = sheet.rowIterator();
        ArrayList list = new ArrayList();
        int rowCount = 0;
        int cellCount = 0 ;
        while(ir.hasNext())
        {
            row = (HSSFRow)ir.next();
            rowCount++;
            
            if(rowCount==1)   //表头 
            {
                for(int i=0;i<row.getPhysicalNumberOfCells();i++)
                {
                    String head = PoiUtil.getCellValue(row,i,"");
                    boolean bMust ;
                    if(head==null||head.equalsIgnoreCase(""))
                    {
                        throw new BizException("表头["+(cellCount+1)+"]未定义");
                    }
                    if(head.charAt(0)=='*')
                    {
                        bMust = true ;
                        head = head.substring(1);
                    }
                    else
                    {
                        bMust = false ;
                    }
                    mColumns.add(new ExcelColumn(head,bMust));
                }
                cellCount = row.getPhysicalNumberOfCells();
            }
            else             //数据
            {
                String[] values = new String[cellCount];
                for(int i=0;i<cellCount;i++)
                {
                    String value = PoiUtil.getCellValue(row,i,"");
                    if(isMustColumn(i) && (value==null||value.equalsIgnoreCase("")))
                    {
                        throw new BizException("第"+rowCount+"行["+getColumnName(i)+"]没有值.");
                    }
                    values[i] = value ; 
                }
                mRows.add(values);
            }
        }
    }
    
    public int getRowNum()
    {
        return mRows.size();
    }

    public String getString(int rowIndex,String name)
    {
        int idx = getColumnIndex(name);
        String[] row = (String[])mRows.get(rowIndex); 
        String returnValue = row[idx];
        if(returnValue != null && returnValue.length() != 0)
            return returnValue.trim();
        else
            return returnValue;
    }
    
    public int getInt(int row,String name,int def)
    {
         try
         {
            return Integer.parseInt(getString(row,name));
         }
         catch(Exception e)
         {
            return def ;
         }
    }
    public int getInt(int row,String name) throws Exception
    {
        return Integer.parseInt(getString(row,name));
    }
    public double getDouble(int row,String name,double def)
    {
        try
        {
           return Double.parseDouble(getString(row,name));
        }
        catch(Exception e)
        {
           return def ;
        }
    }
    public double getDouble(int row,String name) throws Exception 
    {
        return Double.parseDouble(getString(row,name));
    }
}
